package cn.edu.swpu.cins.u_chain.frontend.service.team.Impl;

import cn.edu.swpu.cins.u_chain.frontend.dao.team.TeamDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.team.TeamDetailInfos;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.team.TeamDetails;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.team.TeamMemView;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.team.TeamPros;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.student.SkillDetails;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.team.TeamInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.team.TeamMembers;
import cn.edu.swpu.cins.u_chain.frontend.enums.TeamRole;
import cn.edu.swpu.cins.u_chain.frontend.service.team.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamDao teamDao;
    @Value("${u_chain.size}")
    private int size;
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, IOException.class, SQLException.class})
    public int addTeam(TeamInfo teamInfo, int userId) {
        teamInfo.setCreatorId(userId);
        teamInfo.setCreateDate(new Date().getTime());
        teamDao.insertTeam(teamInfo);
        int teamId = teamInfo.getTeamId();
        TeamMembers teamMembers = new TeamMembers();
        teamMembers.setIsManager(TeamRole.OWNER.getRole());
        teamMembers.setJoinDate(new Date().getTime());
        teamMembers.setTeamId(teamId);
        teamMembers.setUserId(userId);
        return teamDao.insertTeamMember(teamMembers);
    }

    @Override
    public List<TeamPros> getTeamProsByUserId(int userId, int pageNum) {
        List<Integer> teamIds = teamDao.selectTeamIdsByUserId(userId);
        //此时userId应该要转化为teamId
        int offset = --pageNum * size;
        //可以用到范围查询
        return teamDao.selectProByTeamId(teamIds,size,offset);
    }

    /**
     *  查询成员信息 并且查询成员技能信息 还有团队的项目列表
     */
    @Override
    public TeamDetailInfos selectTeamDetailInfoById(int teamId) {
        //团队所有成员信息的封装
        List<TeamDetails> teamDetails = new ArrayList<>();
        //团队成员信息列表
        List<TeamMemView> memViewList = teamDao.selectAllMems(teamId);
        memViewList.stream().forEach(teamMemView -> {
            int userId = teamMemView.getOwnId();
            //团队每个人的技能信息列表
            List<SkillDetails> skillDetailsList = teamDao.selectMemSkillsById(userId);
            teamDetails.add(new TeamDetails(teamMemView, skillDetailsList));
        });
        //团队所有项目信息
        List<TeamPros> teamProsList = teamDao.selectProByOneTeamId(teamId);
        //层级嵌套比较复杂 还是stream比较好用
        TeamDetailInfos teamDetailInfos = new TeamDetailInfos(teamProsList, teamDetails);
        return teamDetailInfos;
    }

//    public List<Integer> intToList(int teamId) {
//        List<Integer> list = new ArrayList<>();
//        list.add(teamId);
//        return list;
//    }
}
